package cn.tedu.ximart.server.controller;

import cn.tedu.ximart.server.pojo.dto.CategoryAddNewDTO;
import cn.tedu.ximart.server.pojo.dto.CategoryEditDTO;
import cn.tedu.ximart.server.pojo.vo.CategoryListItemVO;
import cn.tedu.ximart.server.service.ICategoryService;
import cn.tedu.ximart.server.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "分类管理模块")
@RestController
@RequestMapping("/categories")
public class CategoryController {

    @Autowired
    public ICategoryService categoryService;

    public CategoryController() {
        log.debug("CategoryController的构造方法被调用了");
    }

    @ApiOperation("增加分类")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    @PreAuthorize("hasAnyAuthority('/pms/product/update','/pms/product/read')")
    public JsonResult addNew(@RequestBody CategoryAddNewDTO categoryAddNewDTO) {
        log.debug("{}", categoryAddNewDTO);
        log.debug("CategoryController.addNew");
        categoryService.addNew(categoryAddNewDTO);
               return JsonResult.ok();
    }

    @ApiOperation("根据id删除分类")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAnyAuthority('/pms/product/delete','/pms/product/read')")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable Long id) {
        log.debug("CategoryController.delete ={}", id);
        categoryService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id修改分类")
    @ApiOperationSupport(order = 201)
    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAnyAuthority('/pms/product/update','/pms/product/read')")
    public String updateById(@PathVariable Long id, CategoryEditDTO categoryEditDTO) {
        log.debug("CategoryController.updateById{}", id);
        return "即将根据id修改类别信息（尚未完成）";
    }

    @ApiOperation("查询分类列表")
    @ApiOperationSupport(order = 401)
    @GetMapping("")
    @PreAuthorize("hasAnyAuthority('/pms/product/update','/pms/product/read','/pms/product/delete')")
    public JsonResult list(){
        List<CategoryListItemVO> categories = categoryService.getCategories();
        return JsonResult.ok(categories);
    }


}
